package com.zkyc.framework.website.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zkyc.framework.common.pojo.File;
import com.zkyc.framework.common.utils.EasyExcelUtil;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.common.vo.website.FileDownloadCountVo;
import com.zkyc.framework.common.vo.website.MediaVo;
import com.zkyc.framework.website.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("file")
public class FileController {

    @Autowired
    private MediaService mediaService;

    @GetMapping("queryById")
    @HystrixCommand(fallbackMethod = "queryByIdFallback")
    public ResultVO queryById(Integer id) {
        return ResultVO.data(mediaService.queryById(id));
    }

    public ResultVO queryByIdFallback(Integer id) {
        return ResultVO.success("访问超时,请稍后再试");
    }


    @PostMapping("mediaList")
    public ResultVO queryMediaList(@RequestBody MediaVo media) {
        return mediaService.queryList(media);
    }

    @PostMapping("save")
    public ResultVO add(@RequestBody MediaVo mediaVo) {
        return mediaService.insert(mediaVo);
    }

    @PostMapping("edit")
    public ResultVO edit(@RequestBody MediaVo mediaVo) {
        return mediaService.update(mediaVo);
    }

    @GetMapping("downloadCount")
    public ResultVO downloadCount(String id) {
        return mediaService.dowanloadCount(id);
    }

    @GetMapping("queryByType")
    public ResultVO queryByType(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size, String type) {
        return mediaService.queryByType(current, size, type);
    }

    @GetMapping("/exportDownloadCount")
    public void export(HttpServletResponse response,@RequestParam("ids") List<Long> ids) {
        List<FileDownloadCountVo> fileDownloadCountVos = mediaService.selectByIds(ids);
        EasyExcelUtil.download(response, fileDownloadCountVos, FileDownloadCountVo.class, "文件下载量");
    }



}
